package com.lishe.lishe.lishe.MyRecyclerView;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shino on 2017/9/22.
 */

public abstract class RecyclerviewBaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private View headerView;
    private View footerView;
    private View refreshHeaderView;
    private View refreshFooterView;

    public static final int TYPE_HEADER = -10000;
    public static final int TYPE_FOOTER = 10000;
    public static final int TYPE_REFRESH_HEADER = -10001;
    public static final int TYPE_REFRESH_FOOTER = 10001;
    private Map<Integer, View> headView = new HashMap<>();
    private Map<Integer, View> headView0 = new HashMap<>();//默认的添加
    List<Integer> types = new ArrayList<>();
    List<Integer> types0 = new ArrayList<>();
    private Map<Integer, View> footViews = new HashMap<>();
    private Map<Integer, View> footViews0 = new HashMap<>();//默认的添加
    List<Integer> foot = new ArrayList<>();
    List<Integer> foot0 = new ArrayList<>();
//------------------------------------头部底部view--------------------------------

    public void addHeaderView(View headerView) {
        this.headerView = headerView;
        headView0.put(TYPE_HEADER, headerView);
        types0 = null;
        types0 = new ArrayList<>(headView0.keySet());
    }

    public void addHeaderView(View headerView, int type) {
        if (headerView == null) {
            Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView headerView can,t =null");
            return;
        }
        switch (type) {
            case TYPE_HEADER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView type can,t =" + TYPE_HEADER);
                break;
            case TYPE_FOOTER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView type can,t =" + TYPE_FOOTER);
                break;
            case TYPE_REFRESH_HEADER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView type can,t =" + TYPE_REFRESH_HEADER);
                break;
            case TYPE_REFRESH_FOOTER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView type can,t =" + TYPE_REFRESH_FOOTER);
                break;
            case 0:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addHeaderView type can,t =" + 0);
                break;
            default:
                Log.e("default", "添加");
                headView.put(type, headerView);
                types = null;
                types = new ArrayList<>(headView.keySet());
                break;
        }

    }

    public View getHeaderView0() {
        return headerView;
    }

    public Map<Integer, View> getHeaderView() {
        return headView;
    }
    public View getHeaderView(int key) {
        return headView.get(key);
    }

    public void addRefreshHeaderView(View refreshHeaderView) {
        this.refreshHeaderView = refreshHeaderView;
        headView0.put(TYPE_REFRESH_HEADER, refreshHeaderView);
        types0 = null;
        types0 = new ArrayList<>(headView0.keySet());
    }

    public View getRefreshHeaderView() {
        return refreshHeaderView;
    }


    public void addFootView(View footView) {
        this.footerView = footView;
        footViews0.put(TYPE_FOOTER, footView);
        foot0 = null;
        foot0 = new ArrayList<>(footViews0.keySet());
    }

    public void addFootView(View footView, int type) {
        if (footView == null) {
            Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView footView can,t =null");
            return;
        }
        switch (type) {
            case TYPE_HEADER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView type can,t =" + TYPE_HEADER);
                break;
            case TYPE_FOOTER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView type can,t =" + TYPE_FOOTER);
                break;
            case TYPE_REFRESH_HEADER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView type can,t =" + TYPE_REFRESH_HEADER);
                break;
            case TYPE_REFRESH_FOOTER:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView type can,t =" + TYPE_REFRESH_FOOTER);
                break;
            case 0:
                Log.e("RecyclerviewBaseAdapter", "RecyclerviewBaseAdapter.class method addFootView type can,t =" + 0);
                break;
            default:
                Log.e("null", "default");
                this.footViews.put(type, footView);
                foot = null;
                foot = new ArrayList<>(this.footViews.keySet());
                break;
        }
    }

    public View getFootView0() {
        return footerView;
    }

    public Map<Integer, View> getFootView() {
        return footViews;
    }

    public void addRefreshFootView(View refreshFootView) {
        this.refreshFooterView = refreshFootView;
        footViews0.put(TYPE_REFRESH_FOOTER, refreshFootView);
        foot0 = null;
        foot0 = new ArrayList<>(footViews0.keySet());
    }

    public View getRefreshFootView() {
        return refreshFooterView;
    }

    public final void removeHeaderRefreshView() {
        refreshHeaderView = null;
    }

    public final void removeFooterRefreshView() {
        refreshFooterView = null;
    }

    public final boolean removeHeaderView(int key) {
        View v = headView.get(key);
        if(v!=null){
            types.remove(v);
            headView.remove(key);
            notifyDataSetChanged();
            return true;
        }
        return false;
    }

    public final void removeFooterView() {
        footerView = null;
    }


    //--------------------------------recycleview 的原有函数-使用final修饰-----------------------------------------
    @Override
    public final int getItemViewType(int position) {

        int headerCount = 0;
        if (types0 != null) {
            headerCount = headerCount + types0.size();
        }
        if (types != null) {
            headerCount = headerCount + types.size();
        }
        if (types0 != null) {
            if (position < types0.size()) {
                if (types0.size() == 1) {
                    return types0.get(0);
                }
                if (types0.size() == 2) {
                    if (position == 0) {
                        return TYPE_REFRESH_HEADER;
                    }
                    if (position == 1) {
                        return TYPE_HEADER;
                    }
                }
            } else {
                if (types != null) {
                    if ((position - types0.size()) < types.size()) {
                        return types.get(position - types0.size());
                    }
                }
            }
        } else {
            if (types != null) {
                if (position < types.size()) {
                    return types.get(position);
                }
            }
        }

        if (foot0 != null) {
            int index = (foot0.size() + position) - getItemCount();
            if (index >= 0 && index < foot0.size()) {
                if (foot0.size() == 1) {
                    return foot0.get(0);
                }
                if (foot0.size() == 2) {
                    if (index == 0) {
                        return TYPE_FOOTER;
                    }
                    if (index == 1) {
                        return TYPE_REFRESH_FOOTER;
                    }
                }
            } else {
                if (foot != null) {
                    int index1 = (foot.size() + foot0.size() + position) - getItemCount();
                    if (index1 >= 0 && index1 < foot.size()) {
                        return foot.get(index1);
                    }
                }
            }
        } else {
            if (foot != null) {
                int index = foot.size() + position - getItemCount();
                if (index >= 0 && index < foot.size()) {
                    return foot.get(index);
                }
            }
        }
        return getItemViewTypeMy(position - headerCount);
    }


    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (foot0.contains(viewType)) {
            return new ViewHolder(footViews0.get(viewType));
        }
        if (types0.contains(viewType)) {
            return new ViewHolder(headView0.get(viewType));
        }
        if (foot.contains(viewType)) {
            return new ViewHolder(footViews.get(viewType));
        }
        if (types.contains(viewType)) {
            return new ViewHolder(headView.get(viewType));
        }
        return onCreateViewHolderMy(parent, viewType);
    }

    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int headerCount = 0;
        if (types != null) {
            headerCount = headerCount + types.size();
        }
        if (types0 != null) {
            headerCount = headerCount + types0.size();
        }
        //type等于头部或底部的type册不调用onBindViewHolderMy
        int type = getItemViewType(position);
        if (types.contains(type) || types0.contains(type) || foot.contains(type) || foot0.contains(type)) {
            return;
        }
        setOnItemListener(holder, position - headerCount);
        onBindViewHolderMy(holder, position - headerCount);
    }

    @Override
    public final int getItemCount() {
        int itemCount = 0;

        if (foot0 != null) {
            itemCount = itemCount + foot0.size();
        }
        if (foot != null) {
            itemCount = itemCount + foot.size();
        }
        if (types0 != null) {
            itemCount = itemCount + types0.size();
        }
        if (types != null) {
            itemCount = itemCount + types.size();
        }
        int count = getItemCountMy();
        return count + itemCount;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    if (type == TYPE_HEADER || type == TYPE_FOOTER || type == TYPE_REFRESH_FOOTER || type == TYPE_REFRESH_HEADER) {
                        return gridManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }
    }

    //------------------------对应recycleview必须实现的三个抽象方法-----final修饰了原有的方法，防止误用----------------------

    public abstract RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType);

    public abstract void onBindViewHolderMy(RecyclerView.ViewHolder holder, int position);

    public abstract int getItemCountMy();

    public abstract int getItemViewTypeMy(int position);

    //实现点击事件
    public void setOnItemListener(final RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClicListener != null) {
                    itemClicListener.OnClickListener(RecyclerviewBaseAdapter.this, holder.itemView, position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (itemLongClickListener != null) {
                    return itemLongClickListener.OnLongClickListener(RecyclerviewBaseAdapter.this, holder.itemView, position);
                }
                return false;
            }
        });
    }

    /**
     * 头部viwe 的holder
     */
    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            //----------------避免存在parent，导致recycleview，addView报错------------------------
            ViewGroup view = (ViewGroup) itemView.getParent();
            if (view != null) {
                view.removeAllViews();
            }
        }
    }

    //---------------------------------------------点击监听事件---------------------------------
    ItemListener.ItemClickListener itemClicListener;
    ItemListener.ItemLongClickListener itemLongClickListener;

    public interface ItemListener {
        interface ItemClickListener {
            void OnClickListener(RecyclerView.Adapter adapter, View view, int position);
        }

        interface ItemLongClickListener {
            boolean OnLongClickListener(RecyclerView.Adapter adapter, View view, int position);
        }
    }

    public void setOnItemClickListener(ItemListener.ItemClickListener itemClicListener) {
        this.itemClicListener = itemClicListener;
    }

    public void setOnItemLongClickListener(ItemListener.ItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    /**
     * 若是在adapter中有实现了长短点击事件，请在点击实践中实现这两个方法
     */
    public final void setOnItemClickListener(RecyclerviewBaseAdapter adapter, final RecyclerView.ViewHolder holder, final int position) {
        if (itemClicListener != null) {
            itemClicListener.OnClickListener(adapter, holder.itemView, position);
        }
    }

    public final boolean setOnItemLongClickListener(final RecyclerView.ViewHolder holder, final int position) {
        if (itemLongClickListener != null) {
            return itemLongClickListener.OnLongClickListener(RecyclerviewBaseAdapter.this, holder.itemView, position);
        }
        return false;
    }
}
